﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using SS4U.Library.ConfigurationClasses;
using umbraco;
using umbraco.uicontrols;

namespace SS4U.Section.Configuration
{
    public partial class EditSearchDefinition : Page
    {
        private TabPage firstTab;
        private ConfigurationRoot configRoot;
        private SearchDefinition searchDefinition;

        protected void Page_Load(object sender, EventArgs e)
        {
            var id = Request["id"];
            configRoot = Library.Configuration.Root;
            searchDefinition = configRoot.SearchDefinitions.GetByName(id);

            if (!IsPostBack)
            {
                foreach (var name in Enum.GetNames(typeof(IterationType)))
                    cboIterationType.Items.Add(new ListItem(name, name));

                // Load initial values

                txtName.Text = searchDefinition.Name;
                cboIterationType.Items.FindByText(Enum.GetName(typeof(IterationType), searchDefinition.IterationType)).Selected = true;
                chkIsDefault.Checked = searchDefinition.IsDefault;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            // Tab setup
            firstTab = tabControl.NewTabPage("Search Definition");
            firstTab.Controls.Add(Pane1);

            ////save button
            ImageButton save = firstTab.Menu.NewImageButton();
            save.Click += SaveClick;
            save.AlternateText = "Save";
            save.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";

        }

        private void SaveClick(object sender, ImageClickEventArgs e)
        {
            searchDefinition.Name = txtName.Text;
            searchDefinition.IsDefault = chkIsDefault.Checked;
            searchDefinition.IterationType = (IterationType)Enum.Parse(typeof(IterationType), cboIterationType.SelectedValue);
         
            Library.Configuration.SaveConfiguration();
        }
    }
}
